package com.kgc.cgconsumer.Controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgconsumer.service.CGConServ;
import com.kgc.cgconsumer.service.CgWorkFlowService;
import com.kgc.cgconsumer.service.GYBJConsumerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/GYBJ")
@RestController
@CrossOrigin
public class GYBJController {

    private Logger logger = LoggerFactory.getLogger(GYBJController.class);

    @Resource
    private GYBJConsumerService gybjServ;

    @Resource
    private CGConServ cgConServ;

    private static final String TABLE = "cg_cgybj";

    @Resource
    private CgWorkFlowService cgWorkFlowService;

    @RequestMapping("/getPage")
    public ResponseMsg getPage(@RequestBody Map<String, Object> param) {
        ResponseMsg responseMsg = gybjServ.getGYBJPage(param);
        return responseMsg;
    }

    @RequestMapping("addGYBJ")
    public ResponseMsg addGYBJ(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = gybjServ.addGYBJ(map);

        Map<String,Object> variables = new HashMap<>();
        variables.put("code",responseMsg.getData());//单据编号
        variables.put("type",map.get("type"));//流程类型
        //创建一个流程实例
        com.kgc.activiti.util.ResponseMsg res = cgWorkFlowService.startProcess(variables);
        if (!"200".equals(res.getCode())){
            responseMsg.setCode(res.getCode());
        }

        return responseMsg;
    }
    @RequestMapping("delGYBJ")
    public ResponseMsg delGYBJ(@RequestParam String code){
        ResponseMsg responseMsg = cgConServ.del(TABLE, code);
        return responseMsg;
    }
    @RequestMapping("modiGYBJ")
    public ResponseMsg modiGYBJ(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = gybjServ.modiGYBJ(map);
        return responseMsg;
    }
    @RequestMapping("getGYBJ")
    public ResponseMsg getGYBJ(@RequestParam String code){
        logger.info("getGYBJ code:{}",code);
        ResponseMsg responseMsg = gybjServ.getGYBJ(code);
        return responseMsg;
    }
    @RequestMapping("getGYBJByCGXJ")
    public ResponseMsg getGYBJByCGXJ(@RequestParam String xj_code){
        ResponseMsg responseMsg = gybjServ.getGYBJByCGXJ(xj_code);
        return responseMsg;
    }
    @RequestMapping("/check")
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("CGXJController check params,{}",map.toString());
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",map.get("code"));//单据编号
        variables.put("checkManId",Integer.valueOf((String) map.get("checkManId")));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cgWorkFlowService.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }
}
